//
//  UserInfoTableViewController.m
//  PhotographyStudi
//
//  Created by 叶一枫 on 2017/6/1.
//  Copyright © 2017年 叶一枫. All rights reserved.
//

#import "UserInfoTableViewController.h"

@interface UserInfoTableViewController ()<UITableViewDelegate,UITableViewDataSource,UIGestureRecognizerDelegate,UITextFieldDelegate>

@property(nonatomic,strong)NSArray *itemArr;
@property(nonatomic,strong)NSArray *valueArr;
@property(nonatomic,strong)UILabel *detailLabel;//个性签名特殊处理,需要折行
@property(nonatomic,strong)UIImage *selectedImage;//选择后的头像

@end

@implementation UserInfoTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.itemArr = @[@"名称:",@"摄影工作室:",@"性别:",@"年龄:",@"微信:",@"个性签名:"];
    self.valueArr = @[@"Black_Star",@"暗星摄影师_Cocobar",@"男",@"29",@"Black_Star",@"Not every morning wake up your alarm clock , but Dream"];
    
    self.tableView.tableFooterView = [[BaseView alloc]init];
}

-(UILabel *)detailLabel{
    
    if (!_detailLabel) {
        _detailLabel = [[UILabel alloc]initWithFrame:CGRectMake(120, 0, VIEW_W - 155, 44)];
        _detailLabel.textColor = [UIColor grayColor];
        _detailLabel.font = [UIFont systemFontOfSize:16];
        _detailLabel.numberOfLines = 0;
        _detailLabel.textAlignment = 2;
    }
    return _detailLabel;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    NSArray *rowNum = @[@1,@6];
    return [rowNum[section]integerValue];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        return 80;
    }
    if (indexPath.row == 5) {
        
        CGSize size = [[BSTool sharedBSTool]getSizeWithStr:self.valueArr[5] size:VIEW_W-155 font:[UIFont systemFontOfSize:16] height:YES];
        self.detailLabel.height = size.height>=44?size.height:44;
        return size.height>=44?size.height:44;
    }
    return 44;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    if (section == 0) {
        return 20;
    }
    return 0.1;
}



-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    BaseTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[BaseTableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell" UIType:DETAILCELLSINGLE];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    
    if (indexPath.section == 0) {
        SettingHeaderCell *cell = [[SettingHeaderCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"headerCell" UIType:BASECELLSINGLE];
        cell.headerAvatar.userInteractionEnabled = YES;
        cell.detailTextLabel.text = @"点击更换头像";
        [cell.headerAvatar sd_setImageWithURL:[NSURL URLWithString:@""] placeholderImage:IMAGE_NAMED(@"defaultAvatar")];
        
        if (self.selectedImage) {
            cell.headerAvatar.image = self.selectedImage;
        }
        
        cell.tapImage = ^{
            //查看头像
        };
        return cell;
    }
    
    cell.textLabel.text = self.itemArr[indexPath.row];
    if (indexPath.row == 5) {
        self.detailLabel.text = self.valueArr[indexPath.row];
        //避免重用
        if (![cell.contentView.subviews containsObject:self.detailLabel]) {
            [cell.contentView addSubview:self.detailLabel];
        }
    }else{
        cell.detailTextLabel.text = self.valueArr[indexPath.row];
    }
    
    return cell;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [super tableView:tableView didSelectRowAtIndexPath:indexPath];
     BaseTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

    if (indexPath.section == 0) {
        WEAKSELF
        [[BSTool sharedBSTool]alertImagePickerWithTitle:@"" des:@"选择方式" complet:^(NSInteger index, id anyone) {
            if(index == 1){
                [[BSSelectImage defaultBSSelect]selectImageFromLibrary:NO type:@"danxuan" viewController:weakSelf];
                [BSSelectImage defaultBSSelect].selectImageBlock = ^(UIImage *selectedImage) {
                    weakSelf.selectedImage = selectedImage;
                    [weakSelf.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationNone];
                };
            }else if (index == 2){
           
                [[BSSelectImage defaultBSSelect]selectImageFromLibrary:YES type:@"danxuan" viewController:weakSelf];
                [BSSelectImage defaultBSSelect].selectImageBlock = ^(UIImage *selectedImage) {
                    weakSelf.selectedImage = selectedImage;

                    [weakSelf.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationNone];
                };
            }else{
            
                
            }
        }];
    }else{
    
        if (indexPath.row == 2) {
            UIAlertAction *sexMan = [UIAlertAction actionWithTitle:@"男" style:0 handler:^(UIAlertAction * _Nonnull action) {
                cell.detailTextLabel.text = @"男";

            }];
            UIAlertAction *sexWoman = [UIAlertAction actionWithTitle:@"女" style:0 handler:^(UIAlertAction * _Nonnull action) {
                cell.detailTextLabel.text = @"女";
            }];

            [[BSTool sharedBSTool]alertWithTitle:@"" des:@"请选择性别" actions:@[sexMan,sexWoman]];
            
        }else{
            [[BSTool sharedBSTool]alertTextFiledTitle:@"" des:[NSString stringWithFormat:@"请输入%@",self.itemArr[indexPath.row]] complet:^(NSInteger index, id anyone) {
                if (index == 1) {
                    UITextField *textF = anyone;
                    if (textF.text.length<=0) {
                        [MBProgessHUD_BS showTextModeInView:self.view andText:@"内容不能为空"];
                    }else{
                        cell.detailTextLabel.text = textF.text;
                    }
                }
            } number:indexPath.row==3?YES:NO];
        }
    }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
